package com.qf.controller;

import com.alipay.api.internal.util.AlipaySignature;
import com.qf.entity.TlGorder;
import com.qf.entity.TlHotelOrder;
import com.qf.mapper.GorderMapper;
import com.qf.service.GorderService;
import com.qf.utils.AliPayTools2;
import com.qf.utils.PayMap;
import com.qf.utils.ResultMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

@RestController
@RequestMapping("/gorder")
public class GorderController {
    @Autowired
    GorderService gorderService;
    @Autowired
    AliPayTools2 aliPayTools2;
    @Autowired
    GorderMapper gorderMapper;
    @GetMapping("/findAll")//根据用户ID查询所有订单
    public ResultMsg findAll(HttpServletRequest request){
        return gorderService.findAll(request);
    }

    @GetMapping("/findOne")//根据订单编号查询订单信息
    public ResultMsg findOne(String gorderId,HttpServletRequest request){
        return gorderService.findOne(gorderId,request);
    }

    @GetMapping("/findAllByFlagZero")//查询未支付订单
    public ResultMsg findAllByFlagZero(HttpServletRequest request){
        return gorderService.findAllByFlagZero(request);
    }

    @GetMapping("/moveById")//取消订单
    public ResultMsg moveById(String gorderId){
        return gorderService.moveById(gorderId);
    }

    @GetMapping("/findAllByFlagOne")//查询已支付(待收货)订单
    public ResultMsg findAllByFlagOne(HttpServletRequest request){
        return gorderService.findAllByFlagOne(request);
    }

    @GetMapping("/findAllByFlagTwo")//查询已取消订单
    public ResultMsg findAllByFlagTwo(HttpServletRequest request){
        return gorderService.findAllByFlagTwo(request);
    }

    @GetMapping("/insert")//点击购买生成订单
    public ResultMsg insert(Integer gid,Integer num, String addr, HttpServletRequest request){
        return gorderService.insert(gid,num,addr,request);
    }


    @PostMapping("/insertAll")//点击结算生成订单
    public ResultMsg insertAll(@RequestBody YourDTO yourDTO,HttpServletRequest request){
        // 将 List 转换为数组
//        List<Integer> gid = yourDTO.getGid();
//        Integer[] gids = gid.toArray(new Integer[0]);
//        List<Integer> num = yourDTO.getNum();
//        Integer[] nums = num.toArray(new Integer[0]);
//        String addr = yourDTO.getAddr();
        return gorderService.insertAll(yourDTO.getGid().toArray(new Integer[0]),yourDTO.getNum().toArray(new Integer[0]),yourDTO.getAddr(),request);
    }

    @GetMapping("/deleteById")//根据订单编号删除订单
    public ResultMsg deleteById(String gorderId){
        return gorderService.deleteById(gorderId);
    }

    @PostMapping("/phonetopay")//手机端支付
    public ResultMsg phonetopay(@RequestBody Map map){
        String gorderId = (String) map.get("gorderId");
        Integer gorderPrice = (Integer) map.get("gorderPrice");
        String order = aliPayTools2.pay(gorderId, "商品订单", gorderPrice,"/gorder/notify");
        return ResultMsg.SUCCESS(order);
    }
    @PostMapping("/hotelordertopay")
    public ResultMsg topay(@RequestBody Map map){
        String gorderId = (String) map.get("gorderId");
        Integer gorderPrice = (Integer) map.get("gorderPrice");
        String order = aliPayTools2.saoma(gorderId, "商品订单", gorderPrice,"/gorder/notify");
        return ResultMsg.SUCCESS(order);
    }

    @PostMapping("/notify/{gid}")
    public ResultMsg goodsnotify(@PathVariable("gid") String gid, HttpServletRequest request)throws Exception{
        //二次验签：request的值转为map
        Map<String, String> paramsMap = PayMap.converToMap(request);
        //将异步通知中收到的所有参数都存放到 map 中
        boolean  signVerified = AlipaySignature.rsaCheckV1(paramsMap, AliPayTools2.alipayPublicPay, "utf-8", "RSA2");  //调用SDK验证签名
        if (signVerified){
            // TODO 验签成功后，按照支付结果异步通知中的描述，对支付结果中的业务内容进行二次校验，校验成功后在response中返回success并继续商家自身业务处理，校验失败返回failure

            String status = paramsMap.get("trade_status");
            if(status.equals("TRADE_SUCCESS")){
                //订单状态的修改
                TlGorder tlGorder = gorderMapper.selectById(gid);
                tlGorder.setGorderFlag(1);
                gorderMapper.updateById(tlGorder);
            }
            return ResultMsg.SUCCESS();
        } /*else {
            // TODO 验签失败则记录异常日志，并在response中返回failure.
        }
        return null;*/
        throw new RuntimeException("支付验签失败");
    }

    // 定义 DTO 类
    public static class YourDTO {
        private List<Integer> gid;
        private List<Integer> num;
        private String addr;
        public List<Integer> getGid() {
            return gid;
        }
        public void setGid(List<Integer> gid) {
            this.gid = gid;
        }
        public List<Integer> getNum() {
            return num;
        }
        public void setNum(List<Integer> num) {
            this.num = num;
        }
        public String getAddr() {
            return addr;
        }
        public void setAddr(String addr) {
            this.addr = addr;
        }
    }
}
